package com.utils
{
	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * 多功能按钮
	 * @author Shines
	 */
	public class MultiButton extends EventDispatcher
	{
		protected var _clip:MovieClip = null;//剪辑
		protected var _label:TextField = null;//按钮上的文本
		protected var _hasGray:Boolean = false;//是否有灰帧
		protected var _enabled:Boolean = true;//按钮是否可用
		
		protected var _format:TextFormat = new TextFormat();//文本格式
		protected var _outColor:int = 0;//移出颜色
		protected var _overColor:int = 0;//移入颜色
		protected var _downColor:int = 0;//按下颜色
		protected var _disabledColor:int = 0;//不可用颜色
		protected var _isSetColor:Boolean = false;//是否设置颜色
		
		/**
		 * 构造
		 * @param	aClip	剪辑
		 * @param	hasGray	是否有灰帧
		 */
		public function MultiButton(aClip:MovieClip, hasGray:Boolean = false):void
		{
			//设置剪辑
			_clip = aClip;
			_label = _clip.label;
			_clip.gotoAndStop(1);
			_clip.mouseChildren = false;
			_clip.buttonMode = true;
			_hasGray = hasGray;
			
			//添加事件
			addEvents();
		}
		
		/**
		 * 显示
		 */
		public function show():void
		{
			_clip.visible = true;
			addEvents();
		}
		
		/**
		 * 隐藏
		 */
		public function hide():void
		{
			_clip.visible = false;
			removeEvents();
		}
		
		public function setLabelColor(outColor:int = 0, overColor:int = 0, downColor:int = 0, disabledColor:int = 0):void
		{
			_outColor = outColor;
			_overColor = overColor;
			_downColor = downColor;
			_disabledColor = disabledColor;
			_isSetColor = true;
		}
		
		/**
		 * 添加剪辑的“单击”、“移入”、“移出”、“按下”、“弹起”事件
		 */
		private function addEvents():void
		{
			_clip.addEventListener(MouseEvent.CLICK, clickHandler);
			_clip.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
			_clip.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
			_clip.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			_clip.addEventListener(MouseEvent.MOUSE_UP, upHandler);
		}
		
		/**
		 * 移除剪辑的“单击”、“移入”、“移出”、“按下”、“弹起”事件
		 */
		private function removeEvents():void
		{
			_clip.removeEventListener(MouseEvent.CLICK, clickHandler);
			_clip.removeEventListener(MouseEvent.MOUSE_OVER, overHandler);
			_clip.removeEventListener(MouseEvent.MOUSE_OUT, outHandler);
			_clip.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			_clip.removeEventListener(MouseEvent.MOUSE_UP, upHandler);
		}
		
		/**
		 * 剪辑的“单击”事件处理
		 * @param	e
		 */
		private function clickHandler(e:MouseEvent):void
		{
			if (_enabled)
			{
				dispatchEvent(e);
			}
		}
		
		/**
		 * 剪辑的“移入”事件处理
		 * @param	e
		 */
		private function overHandler(e:MouseEvent):void
		{
			if (_enabled)
			{
				if (_isSetColor)
				{
					_format.color = _overColor;
					_label.setTextFormat(_format);
				}
				_clip.gotoAndStop(2);
			}
		}
		
		/**
		 * 剪辑的“移出”事件处理
		 * @param	e
		 */
		private function outHandler(e:MouseEvent):void
		{
			if (_enabled)
			{
				if (_isSetColor)
				{
					_format.color = _outColor;
					_label.setTextFormat(_format);
				}
				_clip.gotoAndStop(1);
			}
		}
		
		/**
		 * 剪辑的“按下”事件处理
		 * @param	e
		 */
		private function downHandler(e:MouseEvent):void
		{
			if (_enabled)
			{
				if (_isSetColor)
				{
					_format.color = _downColor;
					_label.setTextFormat(_format);
				}
				_clip.gotoAndStop(3);
				dispatchEvent(e);
			}
		}
		
		/**
		 * 剪辑的“弹起”事件处理
		 * @param	e
		 */
		private function upHandler(e:MouseEvent):void
		{
			if (_enabled)
			{
				if (_isSetColor)
				{
					_format.color = _overColor;
					_label.setTextFormat(_format);
				}
				_clip.gotoAndStop(2);
			}
		}
		
		/********************* Getter and setter ***********************/
		
		/**
		 * 获取、设置按钮是否可用
		 */
		public function get enabled():Boolean { return _enabled; }
		public function set enabled(value:Boolean):void
		{
			_enabled = value;
			
			//若设置按钮可用，则将剪辑跳转到第1帧，若设置按钮不可用且剪辑有灰帧，则将剪辑跳转到第4帧
			if (_enabled)
			{
				//设置鼠标手形，侦听事件
				_clip.gotoAndStop(1);
				_clip.buttonMode = true;
				addEvents();
			}
			else
			{
				//取消鼠标手形，取消侦听事件
				_clip.buttonMode = false;
				removeEvents();
				
				if (_isSetColor)
				{
					_format.color = _disabledColor;
					_label.setTextFormat(_format);
				}
				if (_hasGray)
				{
					_clip.gotoAndStop(4);
				}
				else
				{
					_clip.gotoAndStop(1);
				}
			}
		}
		
		/**
		 * 获取剪辑
		 */
		public function get clip():MovieClip { return _clip; }
		
		public function get label():String
		{
			if (_label != null)
			{
				return _label.text;
			}
			else
			{
				return null;
			}
		}
		
		public function set label(value:String):void 
		{
			if (_label != null)
			{
				_label.text = value;
			}
		}
	}
}
